package com.longrise.android.byjk.advertisement.admanager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.android.byjk.app.BYJKAppLike;

/**
 * Created by callxkj on 2016/12/20.
 * 缓存一些实体类
 */

public class ADCacheHelper {
    private static ADCacheHelper mInstance;
    private static final String ADCAHCE_SP_BB = "ADCAHCE_SP_BB";
    private final SharedPreferences mSp;

    private ADCacheHelper(){
        mSp = BYJKAppLike.getAPP().getSharedPreferences(ADCAHCE_SP_BB, Context.MODE_PRIVATE);
    }

    public static ADCacheHelper getInstance(){
        if(mInstance==null){
            mInstance = new ADCacheHelper();
        }
        return mInstance;
    }

    private String getString(String key){
        return mSp.getString(key,null);
    }

    private void setString(String key,String value){
        Editor edit = mSp.edit();
        edit.putString(key,value);
        edit.commit();
    }

    /**
     * 保存indexbean
     */
    public void saveADindexBean(ADIndexbean indexbean){
         setString("ad_index_bean"+getUserId(),JSONSerializer.getInstance().Serialize(indexbean));
    }
    public ADIndexbean getADindexBean(){
        ADIndexbean adIndexbean = null;
        String ad_index_bean = getString("ad_index_bean"+getUserId());
        if(!TextUtils.isEmpty(ad_index_bean)){
           adIndexbean = (ADIndexbean) JSONSerializer.getInstance().DeSerialize(ad_index_bean, ADIndexbean.class);
        }
        return adIndexbean;
    }

    /**
     * 保存用户当前的广告区域
     * @param managerzoneno 广告区域
     */
    public void saveManagerzoneno(String managerzoneno){
          setString("managerzoneno"+getUserId(),managerzoneno);
    }
    public String getManagerzoneno(){
        return getString("managerzoneno"+getUserId());
    }


    /**
     * 保存用户唯一id
     * @param userId id
     */
    public void setUserId(String userId){
        setString("userid",userId);
    }
    public String getUserId(){
       return getString("userid");
    }
}
